<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
    <style>

        /* ----------------------------------------------------------------------------------------------------------*/
        /* NavCubePlugin */
        /* ----------------------------------------------------------------------------------------------------------*/

        #myNavCubeCanvas {
            position: absolute;
            width: 250px;
            height: 250px;
            bottom: 50px;
            right: 10px;
            z-index: 200000;
        }

        /* ----------------------------------------------------------------------------------------------------------*/
        /* TreeViewPlugin */
        /* ----------------------------------------------------------------------------------------------------------*/

        #treeViewContainer {
            pointer-events: all;
            height: 100%;
            overflow-y: scroll;
            overflow-x: hidden;
            position: absolute;
            background-color: rgba(255, 255, 255, 0.2);
            color: black;
            top: 80px;
            z-index: 200000;
            float: left;
            left: 0;
            padding-left: 10px;
            font-family: 'Roboto', sans-serif;
            font-size: 15px;
            user-select: none;
            -ms-user-select: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            width: 350px;
        }

        #treeViewContainer ul {
            list-style: none;
            padding-left: 1.75em;
            pointer-events: none;
        }

        #treeViewContainer ul li {
            position: relative;
            width: 500px;
            pointer-events: none;
            padding-top: 3px;
            padding-bottom: 3px;
            vertical-align: middle;
        }

        #treeViewContainer ul li a {
            background-color: #eee;
            border-radius: 50%;
            color: #000;
            display: inline-block;
            height: 1.5em;
            left: -1.5em;
            position: absolute;
            text-align: center;
            text-decoration: none;
            width: 1.5em;
            pointer-events: all;
        }

        #treeViewContainer ul li a.plus {
            background-color: #ded;
            pointer-events: all;
        }

        #treeViewContainer ul li a.minus {
            background-color: #eee;
            pointer-events: all;
        }

        #treeViewContainer ul li a:active {
            top: 1px;
            pointer-events: all;
        }

        #treeViewContainer ul li span:hover {
            color: white;
            cursor: pointer;
            background: black;
            padding-left: 2px;
            pointer-events: all;
        }

        #treeViewContainer ul li span {
            display: inline-block;
            width: calc(100% - 50px);
            padding-left: 2px;
            pointer-events: all;
            height: 23px;
        }

        #treeViewContainer .highlighted-node { /* Appearance of node highlighted with TreeViewPlugin#showNode() */
            border: black solid 1px;
            background: yellow;
            color: black;
            padding-left: 1px;
            padding-right: 5px;
            pointer-events: all;
        }

    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<canvas id="myNavCubeCanvas"></canvas>
<div id="treeViewContainer"></div>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/laserScan.png"/>
    <h1>LASLoaderPlugin & TreeViewPlugin</h1>
    <h2>Making a LiDAR scan appear in the TreeViewPlugin</h2>
    <p>The LASLoaderPlugin's <b>elementId</b> parameter causes
        the entire model to be loaded into a single Entity that gets this ID.</p>
       <p>In conjunction with that parameter, we can also
        use the LASLoaderPlugin's <b>metaModelJSON</b> parameter to create a MetaModel with a MetaObject that corresponds
           to that Entity.</p>
    <p>The TreeViewPlugin is then able to have a node that represents the LiDAR model and controls the
        visibility of that Entity.</p>
    <h3>Stats</h3>
    <ul>
        <li>
            <div id="time">Loading JavaScript modules...</div>
        </li>
    </ul>
    <h3>Components used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/LASLoaderPlugin/LASLoaderPlugin.js~LASLoaderPlugin.html"
               target="_other">LASLoaderPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/TreeViewPlugin/TreeViewPlugin.js~TreeViewPlugin.html"
               target="_other">TreeViewPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/NavCubePlugin/NavCubePlugin.js~NavCubePlugin.html"
               target="_other">NavCubePlugin</a>
        </li>
    </ul>
    <h3>Resources</h3>
    <ul>
        <li><a href="https://geoslam.com/sample-data/" target="_other">Model source</a></li>
    </ul>
</div>
</body>

<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, LASLoaderPlugin, NavCubePlugin, TreeViewPlugin} from "../../dist/xeokit-sdk.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    const cameraControl = viewer.cameraControl;
    const scene = viewer.scene;
    const cameraFlight = viewer.cameraFlight;

    viewer.camera.eye = [-0.5607302486267173, 0.04290712593203087, 4.190729930006354];
    viewer.camera.look = [0.7052946670565493, -0.18264935499372642, -0.14314878939648068];
    viewer.camera.up = [-0.0017317831936537432, 0.9986499061608406, -0.05191691296595461];

    viewer.scene.gammaInput = true;
    viewer.scene.gammaOutput = true;

    //------------------------------------------------------------------------------------------------------------------
    // Create a NavCube
    //------------------------------------------------------------------------------------------------------------------

    new NavCubePlugin(viewer, {
        canvasId: "myNavCubeCanvas",
        visible: true,
        size: 250,
        alignment: "bottomRight",
        bottomMargin: 100,
        rightMargin: 10
    });

    //------------------------------------------------------------------------------------------------------------------
    // Create a tree view
    //------------------------------------------------------------------------------------------------------------------

    new TreeViewPlugin(viewer, {
        containerElement: document.getElementById("treeViewContainer"),
        autoExpandDepth: 3, // Initially expand tree three storeys deep
        hierarchy: "containment"
    });

    //----------------------------------------------------------------------------------------------------------------------
    // Load a model and fit it to view
    //
    // - Specify an `elementId` parameter, which causes the entire model to be loaded into a single Entity that gets this ID.
    // - Specify a `metaModelJSON` parameter, which creates a MetaModel with two MetaObjects, one of which corresponds
    //   to our Entity. Then the TreeViewPlugin is able to have a node that can represent the model and control the
    //   visibility of the Entity.
    //----------------------------------------------------------------------------------------------------------------------

    const lasLoader = new LASLoaderPlugin(viewer);

    const sceneModel = lasLoader.load({ // Creates a SceneModel with ID "myScanModel"
        id: "myScanModel",
        src: "../../assets/models/las/Nalls_Pumpkin_Hill.laz",
        rotation: [-90, 0, 0],

        entityId: "3toKckUfH2jBmd$7uhJHa4", // Creates an Entity with this ID

        metaModelJSON: { // Creates a MetaModel with ID "myScanModel"
            "metaObjects": [
                {
                    "id": "3toKckUfH2jBmd$7uhJHa6", // Creates a MetaObject with this ID
                    "name": "My Project",
                    "type": "Default",
                    "parent": null
                },
                {
                    "id": "3toKckUfH2jBmd$7uhJHa4", // Creates a MetaObject with this ID (same ID as our Entity)
                    "name": "My Scan",
                    "type": "Default",
                    "parent": "3toKckUfH2jBmd$7uhJHa6"
                }
            ]
        }
    });

    const t0 = performance.now();
    document.getElementById("time").innerHTML = "Loading model...";
    sceneModel.on("loaded", function () {
        cameraFlight.jumpTo(sceneModel.aabb);
        const t1 = performance.now();
        document.getElementById("time").innerHTML = "Model loaded in " + Math.floor(t1 - t0) / 1000.0 + " seconds<br>Objects: " + sceneModel.numEntities;
    });

</script>
</html>
